package android.video.online.ui.classify;

import android.video.online.core.BasicView;
import android.video.online.core.HttpCallback;
import android.video.online.http.ServiceManager;
import android.video.online.model.Category;
import android.video.online.model.CourseModel;
import android.video.online.utils.CommonUtils;
import android.video.online.widget.LoadingView;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Jay on 2017/8/13.
 */

public class ClassifyPresenter implements ClassifyContract.Presenter {
    ClassifyContract.View view;

    public ClassifyPresenter(ClassifyContract.View view) {
        this.view = view;
    }

    @Override
    public BasicView getView() {
        return view;
    }

    @Override
    public void loadAllCategory(LoadingView mLoadingView) {
        ServiceManager.getInstance().loadAllCategory(mLoadingView, new HttpCallback() {
                    @Override
                    protected void onFail(Call call, Object... objects) {
                        view.onFailed();
                    }

                    @Override
                    protected void onSuccess(Call call, Response response, String result, Object... objects) {
                        if (response.isSuccessful()) {
                            if (CommonUtils.isRightServerResponse(result)) {
                                List<Category> categoryList = CommonUtils.parseArrHasList(result, Category.class);
                                view.onSuccessView(categoryList);
                            } else {
                                view.onFailed();
                            }
                        } else {
                            view.onFailed();
                        }
                    }
                }

        );
    }

    @Override
    public void classifyItemClick(Category category) {
        view.onChangeContentView(category);
    }

    @Override
    public void loadCurrentCourse(LoadingView mLoadingView, Category category) {
        ServiceManager.getInstance().loadCurrentCourse(mLoadingView, category.getTitle(), category.getKeywords(), new HttpCallback() {
            @Override
            protected void onFail(Call call, Object... objects) {
                view.onFailed();
            }

            @Override
            protected void onSuccess(Call call, Response response, String result, Object... objects) {
                if (response.isSuccessful()) {
                    if (CommonUtils.isRightServerResponse(result)) {
                        List<CourseModel> courseModels = CommonUtils.parseArrHasList(result, CourseModel.class);
                        view.onSuccessCourseView(courseModels);
                    } else {
                        view.onFailed();
                    }
                } else {
                    view.onFailed();
                }
            }
        });
    }
}
